package com.hp.attendance.serlvet.attendance;

import com.alibaba.fastjson.JSON;
import com.hp.attendance.entity.dto.LateDTO;
import com.hp.attendance.service.AttendanceService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/late")
public class AttendanceLateServlet extends HttpServlet {
    private AttendanceService attendanceService = new AttendanceService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取迟到数据
        try {
            List<LateDTO> late = attendanceService.getLate();
            List<Map<String,Object>> list = new ArrayList<>();
            for (LateDTO lateDTO : late) {
                HashMap<String, Object> map = new HashMap<>();
                Long number = lateDTO.getNumber();
                String eName = lateDTO.getEName();
                map.put("value",number);
                map.put("name",eName);
                list.add(map);
            }
            String str = JSON.toJSONString(list);
            System.out.println(str);
            resp.getWriter().write(str);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
